# -*- coding: utf-8 -*-
'''
Created on 2018年6月17日
@author: liaolzl
'''
#1.打印cookie信息
import http.cookiejar,urllib.request
cookie = http.cookiejar.CookieJar()
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
response =opener.open('http://www.baidu.com')
for item in cookie:
    print(item.name+"="+item.value)

#2.使用MozillaCookieJar来保存cookie信息为文本文件,在项目路径下会生成cookie(use Mozilla).txt
import http.cookiejar,urllib.request
filename ="cookie(use Mozilla).txt"
cookie = http.cookiejar.MozillaCookieJar(filename)
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
response =opener.open('http://www.baidu.com')
cookie.save(ignore_discard=True, ignore_expires=True)

#3.使用LWPCookieJar来保存cookie信息为文本文件,在项目路径下会生成cookie(use LWP).txt
import http.cookiejar,urllib.request
filename ="cookie(use LWP).txt"
cookie = http.cookiejar.LWPCookieJar(filename)
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
response =opener.open('http://www.baidu.com')
cookie.save(ignore_discard=True, ignore_expires=True)

#4.使用LWP方式存储cookie信息，再读出来
import http.cookiejar,urllib.request
cookie = http.cookiejar.LWPCookieJar()
cookie.load('cookie(use LWP).txt',ignore_discard=True,ignore_expires=True)
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
response = opener.open('http://www.baidu.com')
print(response.read().decode('utf-8'))